<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/19
 * Time: 0:11
 */

namespace app\index\controller;


use app\index\model\SendMessageModel;
use think\Controller;

class Register extends Controller
{
    public function register()
    {
        if(request()->isPost()){
            $data = input("post.");

            $data['password'] = md5(md5($data['password']));
            $data['code'] = str_pad(mt_rand(0, 999999), 6, "0", STR_PAD_BOTH);//邀请码
            $data['create_time'] = strtotime(date('Y-m-d h:i:s', time()));//用户注册时间

            try{
                $id = model('UserModel')->add($data);
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
            if($id){
                return json(['code' => 1, 'data' => $id, 'msg' => '注册成功']);
            }else{
                return json(['code' => 1,  'msg' => '注册失败']);
            }
        }else{
            return $this->fetch();
        }
    }


    public function sendRegisterCode()
    {
        $data['register_code'] = str_pad(mt_rand(0, 999999), 6, "0", STR_PAD_BOTH);
        $content = '【同旅同途】' . $data['register_code'] . '为您的验证码，请在5分钟内完成注册。如非本人操作，请忽略。';
        $mobile = input('post.mobile');
//        $code = new SendMessageModel();
//        $info = $code->sendMSM($mobile, $content);
        if ($data) {
            return json(['code' => 1, 'data' => $data, 'msg' => '返回成功']);
        } else {
          return json_encode('获取失败');
        }
    }
}